package com.pirestupppai.widgets.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.Map;

/**
 * 自定义webview
 *
 * @author: xiaoluo
 * @date: 2017-03-17 11:15
 */
public class TupaiWebView extends WebView {
    private Context mContext;
    private OnTitleListener mTitleListener;

    public TupaiWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    public TupaiWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public TupaiWebView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    private void init() {
        WebSettings webSettings = getSettings();
        webSettings.setAllowFileAccess(true);     // 是否可以访问文件
        webSettings.setSupportZoom(true);         // 是否可以变焦
        webSettings.setBuiltInZoomControls(true); // 是否支持缩放
        webSettings.setJavaScriptEnabled(true);   // 支持javascript
        webSettings.setDomStorageEnabled(true);   // 是否启用了DOM storage API
        webSettings.setLoadWithOverviewMode(true);// 是否充满屏幕
        webSettings.setDatabaseEnabled(true);     // 是否开启数据库存储API权限

        setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    loadUrl(url);
                Log.d("webview", "shouldOverrideUrlLoading---" + url);
                return true;
            }

            // 有些网页不支持此功能,暂不用
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//                    String url = request.getUrl().toString();
//                    loadUrl(url);
//                }
//                Log.d("webview", "--shouldOverrideUrlLoading(request)");
//                return true;
//            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }
        });

        setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                mTitleListener.onTitle(title);
                super.onReceivedTitle(view, title);
            }
        });
    }

    @Override
    public void loadUrl(String url) {
        super.loadUrl(url);
    }

    @Override
    public void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
        super.loadUrl(url, additionalHttpHeaders);
    }

    /**
     * 设置cookie
     */
    public void setCookie(String url, String cookie) {
        // 现在的webview具备自动同步cookies, 不需要人工同步,如必须,参照注释
//        if (Build.VERSION.SDK_INT < 21) {
//            CookieSyncManager.getInstance().sync();
//        } else {
//            CookieManager.getInstance().flush();
//        }
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
//        cookieManager.removeSessionCookie();//移除
        cookieManager.setCookie(url, cookie);
    }

    public void setOnTitleListener(OnTitleListener listener) {
        mTitleListener = listener;
    }

    public interface OnTitleListener {
        void onTitle(String title);
    }
}
